<template>
    <div>
        角色权限1
        <div class='system-form-box'>
            <el-form :model="RoleInfo" labelWidth='180px' label-position='left' :rules="rules" ref="ruleFormRef">
                <el-form-item label="角色名称" prop="RoleName">
                    <el-input placeholder='请输入角色名称' v-model="RoleInfo.RoleName" :maxlength="50" :minlength="1" />
                </el-form-item>
                <el-form-item label="角色名称" prop="RoleDesc">
                    <el-input placeholder='请输入角色描述' type="textarea" v-model="RoleInfo.RoleDesc" />
                </el-form-item>
            </el-form>
            <el-form-item class="system-form-box-button">
                <el-button @click='addRoleMan(RoleInfo.RoleName, RoleInfo.RoleDesc, RoleInfo.RoleId)'>添加</el-button>
                <el-button @click="saveChange(editId, RoleInfo.RoleName, RoleInfo.RoleDesc)">保存修改</el-button>
                <el-button @click="getList">刷新</el-button>
            </el-form-item>
        </div>



        <!-- 角色权限界面-->
        <systemForm 
        :data-source="RoleInfo" 
        :role-right="RoleRight" 
        :pagination="pagination" 
        :columns="columns">
            <!-- 修改按钮 -->
            <template #modify="{ row }">
                <el-button @click="changeRoleMan(row.RoleId)">修改</el-button>
            </template>
            <!-- 删除按钮 -->
            <template #delete="{ row }">
                <el-popconfirm @confirm='deleteRoleMan(row.RoleId)' title="确定删除么?">
                    <template #reference>
                        <el-button>删除</el-button>
                    </template>
                </el-popconfirm>
            </template>
            <!-- 权限分配按钮 -->
            <template #authority="{ row }">
                <el-button @click="authority(row.RoleId)">权限设置</el-button>
            </template>

        </systemForm>
        <authorityDialog v-if="dialogGoodsFormVisible" @close="bindCloseDialog" 
        :RoleInfo="getRoleInfo" 
        :roleMan="roleMans"
        :itemId="itemId">
        </authorityDialog>
    </div>
</template>

<script>
import { ElMessage } from 'element-plus'
import systemForm from '../system/components/systemForm.vue'
import authorityDialog from '../system/components/authorityDialog.vue'
import { mockRoleMan, mockPutRoleMan, mockRoleManList, mockDeleteRoleMan } from '../../api/system'
export default {
    data() {
        return {
            dialogGoodsFormVisible: false,
            RoleInfo: [],
            rules: { // 表单校验规则
                RoleName: [
                    {
                        required: true,
                        message: '角色名称为必填项!!!',
                        trigger: 'blur',

                    },
                ],
                RoleDesc: [
                    {
                        required: true,
                        message: '角色描述为必填项!!!',
                        trigger: 'blur',
                    },
                ],
            },
            // 角色权限
            RoleRight:
                [{
                    RoleRightId: 323,//角色权限id
                    RoleId: 23,   ////角色id
                    NodeId: 2333,  //菜单节点id
                }],

            //定义表格信息：表头，列宽等
            columns: [
                {
                    type: 'selection'
                },
                {
                    label: '角色名称',
                    prop: 'RoleName',
                    width: '200px',
                },
                {
                    label: '角色描述',
                    prop: 'RoleDesc',
                    width: '200px',
                },
                {
                    label: '修改',
                    key: 'modify',
                    slot: true,
                },
                {
                    label: '删除',
                    key: 'delete',
                    slot: true,
                },
                {
                    label: '分配角色权限',
                    key: 'authority',
                    slot: true,
                },
            ],
            // 分页配置器
            pagination: {
                total: 50,
                layout: 'total, ->, prev, pager, next, jumper',
                onCurrentChange: this.pageChange,
                currentPages:1,
                pageSize:10,
            },
            indexInfo: null,
            editId: null,
            deleteId: null,
            getRoleInfo: {},
            roleMan: [
                {
                    RoleName: '增删改',
                    RoleDesc: '十多个',
                    RoleId: 66,
                    RoleRightId: 323,//角色权限id
                    // RoleId: 23,   ////角色id
                    NodeId: 2333, //菜单节点id
                },

                { RoleName: '付住宿费', RoleDesc: '的吧v', RoleId: 98, RoleRightId: 22, NodeId: 24234, },

                { RoleName: '挂号费', RoleDesc: '的烦恼和', RoleId: 94, RoleRightId: 22, NodeId: 24234, },

                { RoleName: '放心吧', RoleDesc: '很反感', RoleId: 96, RoleRightId: 22, NodeId: 24234 },

                { RoleName: '功能性改变', RoleDesc: '关系哪个方便', RoleId: 45, RoleRightId: 22, NodeId: 24234 },]
            // 日期搜索框显示数据

        }
    },
    components: {
        systemForm,
        authorityDialog
    },
    created() {
        this.RoleInfo = this.$store.getters['roleManInfo/roleManInfo']

    },
    mounted() {
        this.getList()
        // this.RoleInfo=this.$store.getters['roleManInfo/roleManInfo']
        // // console.log(this.RoleInfo)

    },
    methods: {
        // 获取数据信息

        async getList() {
            const data=await mockRoleManList(this.pageSizes,this.currentPages)
            
            


            // return this.RoleInfo = this.roleMan

            // const formRef = this.$refs.ruleFormRef
            // formRef.validate(async valid => {
            //     if (valid) {
            // const data = await mockRoleManList()
            // const { resultCode, resultInfo } = data
            // if (resultCode === 1) {
            //     console.log("获取数据", resultInfo)
            //     console.log("resultInfo.list", resultInfo.roleManList)
            // return this.RoleInfo = resultInfo.roleManList
            //             }
            //         },
            // //     }
        },

        //添加角色信息
        async addRoleMan(RoleName, RoleDesc, RoleId) {
            if (RoleName && RoleDesc) {
                const RoleId = Math.floor(Math.random() * 90 + 10)
                

                //保存角色信息
                const RoleManInfo = { RoleName, RoleDesc,RoleId }
                // RoleManInfo['RoleId'] = RoleId
                // RoleManInfo['RoleRightId'] = RoleRightId
                // RoleManInfo['NodeId'] = NodeId
                this.roleMan.push(RoleManInfo)
                // const data = await mockRoleMan(RoleName, RoleDesc)
                // const { resultCode, resultInfo, roleMan } = data
                // if (resultCode === 1) {
                //     ElMessage({
                //         message: '添加角色成功!',
                //         type: 'success',
                //     })
                //     this.$store.dispatch('roleManInfo/saveRoleManInfo', roleMan)
                //     this.RoleInfo = this.$store.getters['roleManInfo/roleManInfo']

                // } else {
                //     ElMessage({
                //         message: '添加角色失败!',
                //         type: 'error',
                //     })
                // }
            }
            else {
                ElMessage({
                    message: '角色名称和信息不能为空!',
                    type: 'error',
                })
            }
        },
        //修改角色信息

        //点击修改按钮回显数据
        /**
         * 1.获取当前数据id值
         * 2.改变输入框状态为修改角色信息
         * 3.根据id获取后端数据
         * 4.将后端数据回显到修改角色的输入框内
        */
        changeRoleMan( id) {
            //将id赋值给中转变量
            this.editId = id

            //过滤id获取数据，回显需要修改的数据
            const getRoleMan = this.RoleInfo.find((item) => item.RoleId === id)

            this.RoleInfo.RoleName = getRoleMan.RoleName
            this.RoleInfo.RoleDesc = getRoleMan.RoleDesc
        },

        //保存修改后数，点击保存修改按钮，回到添加角色输入框

        async saveChange(id, RoleName, RoleDesc) {
            // const data = await mockPutRoleMan(id, RoleName, RoleDesc)

            // const { resultCode } = data
            const roleIndex = this.roleMan.findIndex(role => role.RoleId === id)
            this.roleMan[roleIndex].RoleName = RoleName
            this.roleMan[roleIndex].RoleDesc = RoleDesc

            //如果响应成功则，将角色信息回显到文本框
            // if (resultCode === 1) {
            //     ElMessage({
            //         message: '修改数据成功',
            //         type: 'success',
            //     })
            // } else {
            //     ElMessage({
            //         message: '修改数据失败',
            //         type: 'error',
            //     })
            // }
            // // console.log("data.roleMan>>>>", data)
            this.getList()
        },

        //删除角色信息
        async deleteRoleMan(id) {
            // //根据需要删除的角色id传入
            // console.log('RoleId', id)
            // // 获取当前角色信息
            // const data = await mockDeleteRoleMan(id)
            // //结构响应信息
            // const { resultCode, resultInfo } = data
            // //如果响应成功则，将提示删除失败
            // if (resultCode === 1) {
            //     ElMessage({
            //         message: '删除成功',
            //         type: 'error',
            //     })
            // } else {
            //     ElMessage({
            //         message: '删除失败',
            //         type: 'error',
            //     })
            // }
            const index = this.roleMan.findIndex(role => role.RoleId === id)
            if (index !== -1) {
            // 从数组中删除该角色
            this.roleMan.splice(index, 1)
            this.getList()

            return {
                resultCode: 1,
                resultInfo: '删除成功'

            }
        } else {
            return {
                resultCode: 0,
                resultInfo: '该角色不存在'
            }
        }
        },
        //弹窗权限设置按钮
        authority(id) {
            //过滤id的数获取数据，回显需要修改据
            this.getRoleInfo = this.RoleInfo.find((item) => item.RoleId === id)
            this.dialogGoodsFormVisible = true

        },
        //关闭弹窗

        bindCloseDialog(data) {
            console.log(data)
            this.dialogGoodsFormVisible = false
            this.getList()

        }
    }
}


</script>

<style lang="less" scoped>
.system-form-box {
    margin-top: 50px;
    margin: auto;
    width: 600px;
}
</style>